package com.llcbenwu.controller;

import cn.hutool.http.HttpRequest;
import com.llcbenwu.code.ImageCode;
import com.llcbenwu.code.SmsCode;
import com.llcbenwu.config.Result;
import com.llcbenwu.generator.ImageCodeService;
import com.llcbenwu.generator.SmsCodeSendService;
import com.llcbenwu.security.SecurityProperies;
import com.llcbenwu.util.ImageCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.social.connect.web.HttpSessionSessionStrategy;
import org.springframework.social.connect.web.SessionStrategy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;
import sun.dc.pr.PRError;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * @author lilinchun
 * @date 2021/10/26 0026 13:54
 */
@RequestMapping
@RestController
@Api(value = "验证码")
public class VerificationCodeController {


    public static final String SESSION_KEY = "SESSION_KEY_IMAGE_CODE";

    public static final String SESSION_SMS_KEY = "SESSION_KEY_SMS_CODE";


    private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();

    @Resource
    private ImageCodeService imageCodeService;


    @Resource
    private SmsCodeSendService smsCodeSendService;

    @GetMapping("/code/image")
    @ApiOperation(value = "验证码图片")
    public void createCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        ImageCode imageCode= ImageCodeUtil.createImageCode(request);
        ImageCode imageCode= imageCodeService.createImageCode(request);
        //将不能序列化的图片替换
        SmsCode smsCode=new SmsCode(imageCode.getCode(),imageCode.getExpireTime());
        sessionStrategy.setAttribute(new ServletWebRequest(request),SESSION_KEY,smsCode);
        ImageIO.write(imageCode.getImage(),"JPEG",response.getOutputStream());
    }


    @GetMapping("/code/sms")
    @ApiOperation(value = "短信验证码")
    public void createSms(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletRequestBindingException {
        SmsCode smsCode= imageCodeService.createImageSmsCode(request);
        sessionStrategy.setAttribute(new ServletWebRequest(request),SESSION_SMS_KEY,smsCode);
        String mobile=ServletRequestUtils.getRequiredStringParameter(request,"mobile");  //request中必须含有mobile这个属性的字段
        //发送短信验证码(一般使用第三方的)
        smsCodeSendService.CoodeSend(mobile,smsCode.getCode());
    }



}



